﻿using System;
using System.Collections.Generic;
using System.Text;
using Genetic;

namespace HelloWorld {
    class Program {
        class HelloWorld : Algorithm<char> {
            string _target;

            char RandomChar() {
                return _target[Random.Next(_target.Length)];
            }

            public HelloWorld(int size, string target)
                : base(size) {
            
                _target = target;

                for (int i = 0; i < Population.Length; i++) {
                    Population[i].SetLength(_target.Length);
                    for (int j = 0; j < Population[i].Genes.Length; j++) {
                        Population[i].Genes[j] = RandomChar();
                    }
                }
            }

            public override void Mutate(ref Chromosome<char> chromosome) {
                chromosome.Genes[Random.Next(chromosome.Genes.Length)]
                    = RandomChar();
            }

            public override double GetFitness(ref Chromosome<char> chromosome) {

                int fitness = 0;

                for (int i = 0; i < _target.Length; i++) {
                    fitness += Math.Abs((int)chromosome.Genes[i] - (int)_target[i]);
                }

                return fitness;
            }

            public override bool Epoch(int epoch) {
                if (epoch % 2 == 0
                            || GetFitness(ref Population[0]) == 0) {

                    if (Population.Length > 0) {
                        Console.WriteLine(String.Format("{0} - {1}", GetFitness(ref Population[0]), new string(Population[0].Genes)));
                    }
                }

                return true;
            }
        }

        static void Main(string[] args) {
            
            HelloWorld hw = new HelloWorld(200, "Rachel Appel");

            hw.Converge(100000);

            Console.ReadLine();

        }
    }
}